using System;
using System.Web.UI;

namespace Altairis.FluffyCloud.WebCore.DynamicData.FieldTemplates {

    public partial class ForeignKeyField : System.Web.DynamicData.FieldTemplateUserControl {
        private bool _allowNavigation = true;

        public bool AllowNavigation {
            get {
                return _allowNavigation;
            }
            set {
                _allowNavigation = value;
            }
        }

        public override Control DataControl {
            get {
                return HyperLink1;
            }
        }

        public string NavigateUrl {
            get;
            set;
        }

        protected string GetDisplayString() {
            object value = FieldValue;

            if (value == null) {
                return FormatFieldValue(ForeignKeyColumn.GetForeignKeyString(Row));
            }
            else {
                return FormatFieldValue(ForeignKeyColumn.ParentTable.GetDisplayString(value));
            }
        }

        protected string GetNavigateUrl() {
            if (!AllowNavigation) {
                return null;
            }

            if (String.IsNullOrEmpty(NavigateUrl)) {
                return ForeignKeyPath;
            }
            else {
                return BuildForeignKeyPath(NavigateUrl);
            }
        }
    }
}